<?php
$cache_time=10;
$OJ_CACHE_SHARE=false;
require_once("config/path_config.php");
require_once('./include/cache_start.php');
require_once('./include/db_info.inc.php');
require_once('./include/setlang.php');
require_once("./include/const.inc.php");
require_once("./include/my_func.inc.php");

$user=$_GET['user'];
if (!is_valid_user_name($user)){
    $view_errors= "参数错误";
    require("template/bs3/error.php");
    exit(0);
}

$view_title=$user ."@".$OJ_NAME;
$sql="SELECT `school`,`email`,`nick` FROM `users` WHERE `user_id`=?";
$result=pdo_query($sql,$user);
$row_cnt=count($result);
if ($row_cnt == 0){
    $view_errors= "未查找到指定用户";
    require("template/bs3/error.php");
    exit(0);
}

$row=$result[0];
$school=$row['school'];
$email=$row['email'];
$nick=$row['nick'];

// 正确提交次数
$sql="SELECT count(DISTINCT problem_id) as `ac` FROM `solution` WHERE `user_id`=? AND `result`=4";
$result=pdo_query($sql, $user) ;
$row=$result[0];
$AC=$row['ac'];

// 总共提交次数
$sql="SELECT count(solution_id) as `Submit` FROM `solution` WHERE `user_id`=? and  problem_id>0";
$result=pdo_query($sql,$user);
$row=$result[0];
$Submit=$row['Submit'];

// 更新用户正确完成的题目数
$sql="UPDATE `users` SET `solved`='".strval($AC)."',`submit`='".strval($Submit)."' WHERE `user_id`=?";
$result=pdo_query($sql,$user);

// 查找排在该用户前面的人数，计算用户的排名
$sql="SELECT count(*) as `Rank` FROM `users` WHERE `solved`>? and user_type=1";
$result=pdo_query($sql,$AC);
$row=$result[0];
$Rank=intval($row[0])+1;

// 获取用户登陆记录
 if (isset($_SESSION['administrator'])){
     $sql="SELECT user_id,password,ip,`time` FROM `loginlog` WHERE `user_id`=? order by `time` desc LIMIT 0,15";
     $view_userinfo=pdo_query($sql,$user);
}

// 用户解题状态
$sql="SELECT result,count(1) FROM solution WHERE `user_id`=? AND result>=4 group by result order by result";
$result=pdo_query($sql,$user);
$view_userstat=array();
$i=0;
foreach($result as $row){
    $view_userstat[$i++]=$row;
}

$sql= "SELECT UNIX_TIMESTAMP(date(in_date))*1000 md, count(1) c FROM `solution` where  `user_id`=?  group by md order by md desc";
$result=pdo_query($sql,$user);
$chart_data_all= array();
foreach($result as $row){
    $chart_data_all[$row['md']]=$row['c'];
}

$sql = "SELECT UNIX_TIMESTAMP(date(in_date))*1000 md,count(1) c FROM `solution` where  `user_id`=? and result=4 group by md order by md desc ";
$result=pdo_query($sql,$user);
$chart_data_ac= array();
foreach($result as $row){
    $chart_data_ac[$row['md']]=$row['c'];
}

/////////////////////////Template
require("template/".$OJ_TEMPLATE."/userinfo.php");
/////////////////////////Common foot
if(file_exists('./include/cache_end.php'))
	require_once('./include/cache_end.php');
?>

